package com.orm.analysis.service;

import com.orm.analysis.dao.ItemDAO;
import com.orm.analysis.dao.SalesmanDAO;
import com.orm.analysis.dao.SupplierDAO;
import com.orm.analysis.model.*;
import com.orm.analysis.util.GlobalConst;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 10/12/13
 * Time: 7:30 AM
 * To change this template use File | Settings | File Templates.
 */
public class SupplierService   implements GenericService<Supplier> {


    private static Logger log = Logger.getLogger(SupplierService.class);

    private String supplierDataFielDir;

    private SupplierDAO dao;

    private ItemDAO itemDAO;


    public ItemDAO getItemDAO() {
        return itemDAO;
    }

    public void setItemDAO(ItemDAO itemDAO) {
        this.itemDAO = itemDAO;
    }

    public SupplierDAO getDao() {
        return dao;
    }

    public void setDao(SupplierDAO dao) {
        this.dao = dao;
    }

    public String getSupplierDataFielDir() {
        return supplierDataFielDir;
    }

    public void setSupplierDataFielDir(String supplierDataFielDir) {
        this.supplierDataFielDir = supplierDataFielDir;
    }

    public Supplier create(Supplier entity) {
        dao.save(entity);
        return entity;
    }


    public void delete(Object id) {

        dao.delete(id);
    }

    public Supplier update(Supplier entity) {
        dao.update(entity);
             return entity;
         }

    public List<Supplier> list(int page, int size) {
        return dao.list(page, size);
       }


    public Supplier getById(Supplier entity) {
        return dao.getById(entity.getId());
                }

    public Supplier getById(Integer id) {
        return dao.getById(id);
             }



    public void executeImproter() throws ParseException, IOException {
        log.info("File  >>" + supplierDataFielDir);
                      BufferedReader br = null;

                      br = new BufferedReader(new FileReader(supplierDataFielDir));

                      String line = br.readLine();
                        line = br.readLine();

                      while (line != null) {
                          //ID( not need) FIRST_NAME,LAST_NAME,REGISTRATION_DATE,contactInfo, SUPPLIER_CODEBACK_LISTED, suppliers~suppliers
                          Supplier supplier = new Supplier();

                          String[] data = line.split("[,]");



                          supplier.setFirstName(data[0]);
                          supplier.setLastName(data[1]);

                          SimpleDateFormat simpleDateFormat = new SimpleDateFormat(GlobalConst.DATE_PATTERN);

                          supplier.setRegistrationDate(simpleDateFormat.parse(data[2].trim()));

                          if (data[3].trim().length() > 0) {
                              // supplier.setContactInfo(contactInfoDAO.getById(Integer.valueOf((data[3].trim()))));
                              supplier.setContactInfo(new ContactInfo(Integer.valueOf((data[3].trim()))));
                          }


                          supplier.setSupplierCode(data[4]);


                          Character character = data[5].charAt(0);
                          switch (character) {
                              case 'Y':
                                  supplier.setBackListed(true);
                                  break;
                              case 'N':
                                  supplier.setBackListed(false);
                                  break;
                              default:
                                  supplier.setBackListed(false);
                                  break;
                          }
                          dao.save(supplier);
                          if (data.length > 6 && data[6] != null && data[6].length() > 0) {
                              System.out.println("MMMMMMM" + data[6]);
                              List<Item> itemList = new ArrayList<Item>();
                              String[] itemListIdArray = data[6].split("[~]");
                              for (int i = 0; i < itemListIdArray.length; i++) {
                                  System.out.println("MMMMMMM" + itemListIdArray[i]);
                                  Item item = itemDAO.getById(Integer.valueOf(itemListIdArray[i]))    ;
//                                  Item item = new Item(Integer.valueOf(itemListIdArray[i]))    ;
                                  item.getSuppliers().add(supplier);
                                  supplier.getItems().add(item);
//                                  itemList.add(item);
                              }
//                              if (itemList.size() > 0) {
//                                  System.out.print("MMMMMMM" +  itemList.size() );
//                                  supplier.getItems().addAll(itemList);
//                              }
                          }


                          dao.update(supplier);

                          line = br.readLine();

                      }

                      br.close();
            }
}
